package com.summer.core.util;

import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;

import javax.imageio.ImageIO;

public class ImageUploadUtil {

	private final static String UPLOAD_PATH = "/upload";
	private static SimpleDateFormat formater = new SimpleDateFormat("yyyyMMdd");
	
	
	public static String upload(InputStream is,String fileName){
		try {
			final String path = UPLOAD_PATH + "/" + formater.format(new Date());
			final String absPath = FacesUtil.getRealPath(path) ;
			fileName = getName(fileName);
			final String savefile = absPath +"/"+ fileName;
		
			mkdir(absPath);
			
			FileOutputStream out = new FileOutputStream(savefile);
			byte[] buffer = new byte[2048];
			int x = 0;
			while ((x = is.read(buffer)) != -1) {
				out.write(buffer, 0, x);
			}
			is.close();
			out.close();
			
			return ( path +"/"+ fileName) ;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null ;
	}
	/**
	 * 
	 * @Title:        uploadCompress 
	 * @Description:  TODO(图片上传等比例压缩) 
	 * @param:        @param is
	 * @param:        @param fileName
	 * @param:        @return    
	 * @return:       String    
	 * @throws 
	 * @author        Administrator
	 * @Date          2015年12月24日 下午4:46:04
	 */
	
	public static String uploadCompress(InputStream is,String fileName){
		try {
			final String path = UPLOAD_PATH + "/" + formater.format(new Date());
			final String absPath = FacesUtil.getRealPath(path) ;
			fileName = getName(fileName);
			final String savefile = absPath +"/"+ fileName;
		
			mkdir(absPath);
			
			
			BufferedImage input = ImageIO.read(is);
			int width = input.getWidth(null);    // 得到源图宽
			int height = input.getHeight(null);  // 得到源图高
			int w = 1200;
			int h = 600;
			if(width>=1200&&height>=600){
				if(width / height > w/h){
					h = (int) (height * w / width);
				}else{
					w = (int) (width * h / height);
				}
			}
			/*等比例缩放end*/
            BufferedImage inputbig = new BufferedImage(w, h, BufferedImage.TYPE_INT_BGR);
            Graphics2D g = (Graphics2D) inputbig.getGraphics();
            
            g.drawImage(input, 0, 0,w,h,null); //画图
            g.dispose();
            inputbig.flush();
            ImageIO.write(inputbig, "jpg", new File(savefile));
			return ( path +"/"+ fileName) ;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null ;
	}
	
	/**
	 * 根据字符串创建本地目录 并按照日期建立子目录返回
	 * @param path 
	 * @return 
	 */
	private static void mkdir(final String path) {
		
		File dir = new File( path);
		if (!dir.exists()) {
			try {
				dir.mkdirs();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}
	
	/**
	 * 依据原始文件名生成新文件名
	 * @return
	 */
	private static String getName(String fileName) {
		Random random = new Random();
		return "" + random.nextInt(10000)
				+ System.currentTimeMillis() + getFileExt(fileName);
	}
	
	/**
	 * 获取文件扩展名
	 * 
	 * @return string
	 */
	private static String getFileExt(String fileName) {
		return fileName.substring(fileName.lastIndexOf("."));
	}
}
